package cn.tedu.v1_1;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownServiceException;
import java.util.Scanner;

public class Client {
    private Socket socket;
    private PrintWriter pw;

    public Client() {
        try {
            socket=new Socket("192.168.234.204",6666);
        } catch (UnknownServiceException e) {
            System.out.println("友情提示：IP传入错误");
        } catch (ConnectException e) {
            System.out.println("客户端连接失败：建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器失败，提示：检查IP/端口");
        }
    }
    public void start(){
        OutputStreamWriter osw= null;
        try {
            OutputStream os=socket.getOutputStream();
            osw = new OutputStreamWriter(os,"UTF8");
            BufferedWriter bw=new BufferedWriter(osw);
            pw=new PrintWriter(bw,true);

            String str=null;
            System.out.println("对服务器端说话：");
            Scanner input=new Scanner(System.in);
            String line=null;
            while (true){
                line=input.nextLine();
                pw.println(line);
                System.out.println("写出成功");
            }
        } catch (UnknownServiceException e) {
            System.out.println("客户端/服务器端编码集有问题");
        } catch (IOException e) {
            System.out.println("客户端连接服务器端：IO流操作出现错误");
        } finally {
            if (pw!=null)
                pw.close();
        }
    }
    public static void main(String[] args){
        Client client=new Client();
        client.start();
    }
}
